package com.ruoyi.biz.controller;

import com.ruoyi.collect.domain.RecipeCollectionItems;
import com.ruoyi.collect.service.IRecipeCollectionItemsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 菜谱收藏关系Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController("bizRecipesController")
@RequestMapping("/collect/items")
public class RecipeCollectionItemsController extends BaseController
{
    @Autowired
    private IRecipeCollectionItemsService recipeCollectionItemsService;

    /**
     * 查询菜谱收藏关系列表
     */
    @PreAuthorize("@ss.hasPermi('collect:items:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecipeCollectionItems recipeCollectionItems)
    {
        startPage();
        List<RecipeCollectionItems> list = recipeCollectionItemsService.selectRecipeCollectionItemsList(recipeCollectionItems);
        return getDataTable(list);
    }

    /**
     * 导出菜谱收藏关系列表
     */
    @PreAuthorize("@ss.hasPermi('collect:items:export')")
    @Log(title = "菜谱收藏关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecipeCollectionItems recipeCollectionItems)
    {
        List<RecipeCollectionItems> list = recipeCollectionItemsService.selectRecipeCollectionItemsList(recipeCollectionItems);
        ExcelUtil<RecipeCollectionItems> util = new ExcelUtil<RecipeCollectionItems>(RecipeCollectionItems.class);
        util.exportExcel(response, list, "菜谱收藏关系数据");
    }

    /**
     * 获取菜谱收藏关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('collect:items:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(recipeCollectionItemsService.selectRecipeCollectionItemsById(id));
    }

    /**
     * 新增菜谱收藏关系
     */
    @PreAuthorize("@ss.hasPermi('collect:items:add')")
    @Log(title = "菜谱收藏关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecipeCollectionItems recipeCollectionItems)
    {
        return toAjax(recipeCollectionItemsService.insertRecipeCollectionItems(recipeCollectionItems));
    }

    /**
     * 修改菜谱收藏关系
     */
    @PreAuthorize("@ss.hasPermi('collect:items:edit')")
    @Log(title = "菜谱收藏关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecipeCollectionItems recipeCollectionItems)
    {
        return toAjax(recipeCollectionItemsService.updateRecipeCollectionItems(recipeCollectionItems));
    }

    /**
     * 删除菜谱收藏关系
     */
    @PreAuthorize("@ss.hasPermi('collect:items:remove')")
    @Log(title = "菜谱收藏关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(recipeCollectionItemsService.deleteRecipeCollectionItemsByIds(ids));
    }

    //-----------------------------------------------
    @GetMapping("/select/page")
     public Object findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                            RecipeCollectionItems recipeCollectionItems) {
        return recipeCollectionItemsService.findByPage(pageNum, pageSize, recipeCollectionItems);
    }

    @GetMapping("/select/{id}")
    public RecipeCollectionItems selectById(@PathVariable("id") String id) {
        return recipeCollectionItemsService.findById(id);
    }

    @GetMapping("/select")
    public List<RecipeCollectionItems> select() {
        return recipeCollectionItemsService.findAllCollections();
    }

    @DeleteMapping("/delete")
    public String deleteBatch(Integer[] ids) {
        return recipeCollectionItemsService.removeBatch(ids)> 0 ? "批量删除成功" : "批量删除失败";
    }

    @DeleteMapping("/delete/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
         return recipeCollectionItemsService.remove(id) > 0 ? "删除成功" : "删除失败";
    }

    @PostMapping("/insert")
    public String addSave(@RequestBody RecipeCollectionItems recipeCollectionItems) {
         return recipeCollectionItemsService.insertRecipeCollectionItems(recipeCollectionItems) > 0 ? "新增成功" : "新增失败";
    }

}
